package com.xeecos
{
	import com.adobe.serialization.json.JSON;
	import com.adobe.serialization.json.JSONDecoder;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;

	public class ArticlesManager extends EventDispatcher
	{
		private static var _instance:ArticlesManager;
		
		private var _httpLoader:HttpLoader;
		private var _url:String = "http://1.starrynightwalk.sinaapp.com/mobile/ios/app_list.php";
		private var _lasttime:int=0;
		private var _list:Array = [];
		private var _currentList:Array = [];
		private var _currentID:int = 0;
		public function ArticlesManager()
		{
			_httpLoader = new HttpLoader();
			_httpLoader.addEventListener(Event.COMPLETE,onComplete);
			_httpLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			_lasttime = Math.floor(new Date().time/1000);
		}
		public static function get sharedManager():ArticlesManager{
			if(_instance==null){
				_instance = new ArticlesManager;
			}
			return _instance;
		}
		public function getPrevPage(){
			_currentID-=10;
			if(_currentID<0){
				_currentID = 0;
			}
			_currentList = _list.slice(_currentID,Math.min(_list.length-_currentID,_currentID+10));
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		public function getNextPage(){
			if(_currentID+10>=_list.length-1){
				_httpLoader.post(_url,{"time":_lasttime,"isdebug":1,"count":10});
			}else{
				_currentID+=10;
				_currentList = _list.slice(_currentID,Math.min(_list.length,_currentID+10));
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		public function getLastestPage(){
			_httpLoader.post(_url,{"time":Math.floor(new Date().time/1000)+3600*24*30,"isdebug":1,"count":20});
		}
		public function get currentList():Array{
			return _currentList;
		}
		public function getArticle(id:int):Article{
			for(var i in _list){
				if(_list[i].id==id){
					return _list[i];
				}
			}
			return null;
		}
		public function updateArticle(article:Article):void{
			for(var i in _list){
				if(_list[i].id == article.id){
					_list[i] = article;
				}
			}
		}
		private function onComplete(evt:Event):void{
			var json:String = evt.target.data;
			//trace(json);
			var jsonDecoder:JSONDecoder = new JSONDecoder(json,true);
			var obj:Object = jsonDecoder.getValue();
			if(obj.code==0){
				var array:Array = obj.list;
				for(var i in array){
					var article:Article = new Article();
					article.loadData(array[i]);
					addArticle(article);
				}
				
				if(_list.length>0){
					_list = _list.sortOn("time",Array.DESCENDING|Array.NUMERIC);
					_lasttime = _list[_list.length-1].time;
					_currentList = _list.slice(_currentID,Math.min(_list.length-_currentID,_currentID+10));
				}
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function onIOError(evt:IOErrorEvent):void{
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function addArticle(article:Article):void{
			if(!isExistArticle(article.id)){
				_list.push(article);
			}
		}
		private function isExistArticle(id:int):Boolean{
			for(var i in _list){
				if(_list[i].id==id){
					return true;
				}
			}
			return false;
		}
	}
}